home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3410 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  70 lines

  1. Path: nickel.ucs.indiana.edu!gompa
  2. From: gompa@nickel.ucs.indiana.edu (Raghu R. Gompa)
  3. Newsgroups: comp.lang.c
  4. Subject: Simple C program to list contents of a file - help!
  5. Date: 28 Jan 1996 20:04:24 GMT
  6. Organization: Indiana University, Bloomington
  7. Message-ID: <4egks8$nlu@usenet.ucs.indiana.edu>
  8. NNTP-Posting-Host: nickel.ucs.indiana.edu
  9. NNTP-Posting-User: gompa
  10.  
  11.  
  12. I am trying to figure out what is wrong with this
  13. program.  The following is written for dos environment:
  14. "typeme.c"
  15.  
  16. It is supposed to get words from file and list them with
  17. arguments like help me please with the command:
  18.      typeme -f file help me please
  19.  
  20. Depending on the number of arguments, I sometimes getting
  21. strange error messages - system stuck some times.
  22. Please help.  This is part of a project (not a homework)
  23. I am trying to complete. .. Thanks in advance. .. Raghu
  24.  
  25.  
  26. #include "stdio.h"
  27. #include "string.h"
  28.  
  29. FILE *fopen(), *fp;
  30. extern exit();
  31. main(int argc, char *argv[])
  32. {
  33. char first[100], lett, *words[100], filename[15];
  34. int numwords, cont=1;
  35. int i, j, repeat=10, r,k=0,t;
  36. int nrem;
  37. if(*argv[1] == '-') {
  38.     strcpy(filename, argv[2]);
  39.     fp= fopen(filename,"r");
  40.     if(fp == NULL) printf("Can't open %s \n",filename);
  41.     k=0;*words[1]='1';
  42.     while(fscanf(fp, "%s", words[k]) != EOF ) {
  43.     k++;
  44.     }
  45.     fclose(fp);
  46.     numwords=k-1;
  47.     if(argc>2) {
  48.           for(i=3;i<=argc;i++) {
  49.              strcpy(words[k],argv[i]);
  50.              k++;
  51.              }
  52.           }
  53.     numwords=k-1;
  54.    }
  55. else
  56.    {
  57.    numwords=argc-1;
  58.     for(k=0;k<numwords;k++)
  59.           strcpy(words[k],argv[k+1]);
  60.    }
  61.  
  62.    nrem=1;
  63.    for(i=0;i< numwords;i++) {
  64.      printf("%3d. %10s", i+1, words[i]);
  65.      nrem=(i%4);
  66.      if(i>3 && !nrem) printf("\n");
  67.      }
  68. }
  69.  
  70.